/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import etcore.webapp.grouptalk.common.object.Mail;

/**
 * An instance of MailDAO is a data access object used to access Mail, which
 * contains query and update operations
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class MailDAO extends BaseDAO {

	public Mail findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();
		Mail instance = (Mail) session.get(Mail.class.getName(), id);

		return instance;

	}

	public Mail findByNameAndTime(String name, String time) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = " from Mail where name= ? and time=?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, name);
		queryObject.setParameter(1, time);
		Object results = queryObject.uniqueResult();

		if (results != null)
			return (Mail) results;
		else
			return null;

	}

	public long findSizeByProperty(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Mail where " + propertyName
				+ "= ?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findSizeBy2Properties(String propertyName, Object value,
			String propertyName2, Object value2) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Mail where " + propertyName
				+ "= ? and " + propertyName2 + "= ?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setParameter(1, value2);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public List<Mail> findByProperty(String propertyName, Object value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Mail as model where model." + propertyName
				+ "= ? ";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Mail> results = queryObject.list();

		return results;

	}

	public List<Mail> findBy2Properties(String propertyName, Object value,
			String propertyName2, Object value2, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Mail as model where model." + propertyName
				+ "= ? and " + propertyName2 + "= ?  ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setParameter(1, value2);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Mail> results = queryObject.list();

		return results;

	}

	public long searchSizeByProperty(String propertyName, String value) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Mail where " + propertyName
				+ " like '%" + value + "%'";
		Query queryObject = session.createQuery(queryString);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public List<Mail> searchByProperty(String propertyName, String value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Mail where " + propertyName + " like '%"
				+ value + "%'";
		Query queryObject = session.createQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Mail> results = queryObject.list();

		return results;

	}

	public long findAllSize() {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Mail";
		Query queryObject = session.createQuery(queryString);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public List<Mail> findAll(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Mail";
		Query queryObject = session.createQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Mail> results = queryObject.list();

		return results;

	}

	public List<Mail> findAll() {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Mail";
		Query queryObject = session.createQuery(queryString);
		@SuppressWarnings("unchecked")
		List<Mail> results = queryObject.list();

		return results;

	}

}